package kr.hyunist.androidcomplete01saeildamil.ch12;

import java.util.ArrayList;

import kr.hyunist.androidcomplete01saeildamil.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;

public class ListMultiDelEx extends Activity {
	ArrayList<MyMovie> arList;
	MyMovie mv;
	MyMovieListAdapter Adapter;
	ListView list;
	Button btnAllDel;
	Button btnDel;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.movielistex);
		
		arList = new ArrayList<MyMovie>();
		mv = new MyMovie(R.drawable.poster01, "빅맨", R.drawable.kbs, "(월,화)오후 10:00", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster02, "트라이앵글", R.drawable.mbc, "(월,화)오후 10:00", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster03, "닥터 이방인", R.drawable.sbs, "(월,화)오후 10:00", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster04, "유나의 거리", R.drawable.jtbc, "(월,화)오후 09:50", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster05, "골든 크로스", R.drawable.kbs, "(수,목)오후 10:00", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster06, "개과천선", R.drawable.mbc, "(수,목)오후 10:00", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster07, "너희들은 포위됐다", R.drawable.sbs, "(수,목)오후 10:00", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster01, "너희들은 포위됐다", R.drawable.sbs, "(수,목)오후 10:00", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster02, "너희들은 포위됐다", R.drawable.sbs, "(수,목)오후 10:00", false);
		arList.add(mv);
		mv = new MyMovie(R.drawable.poster03, "너희들은 포위됐다", R.drawable.sbs, "(수,목)오후 10:00", false);
		arList.add(mv);
		
		Adapter = new MyMovieListAdapter(this, R.layout.moviecontent, arList);
		
		list = (ListView) findViewById(R.id.movielistview);
		list.setAdapter(Adapter);
				
		btnAllDel = (Button) findViewById(R.id.movielistalldelbtn);
		btnDel = (Button) findViewById(R.id.movielistdelbtn);
		
		btnAllDel.setOnClickListener(mOnClickListener);
		btnDel.setOnClickListener(mOnClickListener);
				
	}
	
	
	OnClickListener mOnClickListener = new View.OnClickListener() {
				
		@Override
		public void onClick(View v) {			
			switch(v.getId()){
			case R.id.movielistalldelbtn:

				for (int i = 0; i < Adapter.checkBoxState.length; i++) {					
					Adapter.checkBoxState[i] = !Adapter.ch.isChecked();
					
					if(Adapter.checkBoxState[i] = !Adapter.ch.isChecked()){
						btnAllDel.setText(R.string.allselectnot);
					} else {
						btnAllDel.setText(R.string.alldelbtnname);
					}
					
				}
			
				Adapter.notifyDataSetChanged();
				
				break;
				
			case R.id.movielistdelbtn:
				if (arList.size() != 0) {
					for (int i = list.getCount() - 1; i >= 0; i--) {
						if (Adapter.checkBoxState[i]) {
							arList.remove(i);
						}
					}
					
					list.clearChoices();
					Adapter.notifyDataSetChanged();
					
					Adapter.checkBoxState = new boolean[arList.size()];
				} 
				
				if(arList.size() == 0){
					btnAllDel.setText(R.string.alldelbtnname);
				}
				
				break;
			}
		}

	};	

}
